<template>
  <div class="file-upload-container">
    <div class="upload-area" @dragover="handleDragOver" @drop="handleDrop">
      <div class="upload-content">
        <UploadOutlined class="upload-icon" />
        <p class="upload-text">点击选择文件或拖拽文件到此处</p>
        <p class="upload-hint">支持常见图片格式，大小不超过5MB</p>
        <a-button type="primary" @click="triggerFileSelect">
          选择文件
        </a-button>
      </div>
      <input 
        ref="fileInput" 
        type="file" 
        multiple 
        accept="image/*" 
        @change="handleFileSelect" 
        class="file-input"
      />
    </div>
    
    <!-- 上传进度 -->
    <div v-if="uploading" class="upload-progress">
      <a-progress :percent="uploadProgress" />
      <p>正在上传...</p>
    </div>
    
    <!-- 上传结果 -->
    <div v-if="uploadedFiles.length > 0" class="uploaded-files">
      <h3>已上传文件</h3>
      <div class="file-list">
        <div 
          v-for="(file, index) in uploadedFiles" 
          :key="index"
          class="file-item"
        >
          <img :src="file.url" :alt="file.name" class="file-preview" />
          <div class="file-info">
            <p class="file-name">{{ file.name }}</p>
            <p class="file-size">{{ formatFileSize(file.size) }}</p>
            <a :href="file.url" target="_blank" class="file-link">查看文件</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { UploadOutlined } from '@ant-design/icons-vue'
import { fileApi } from '@/api'

// 响应式数据
const fileInput = ref<HTMLInputElement | null>(null)
const uploading = ref(false)
const uploadProgress = ref(0)
const uploadedFiles = ref<Array<{name: string, size: number, url: string}>>([])

// 触发文件选择
const triggerFileSelect = () => {
  fileInput.value?.click()
}

// 处理文件选择
const handleFileSelect = (event: Event) => {
  const input = event.target as HTMLInputElement
  if (input.files && input.files.length > 0) {
    uploadFiles(Array.from(input.files))
  }
}

// 处理拖拽
const handleDragOver = (event: DragEvent) => {
  event.preventDefault()
}

// 处理文件拖放
const handleDrop = (event: DragEvent) => {
  event.preventDefault()
  if (event.dataTransfer && event.dataTransfer.files.length > 0) {
    uploadFiles(Array.from(event.dataTransfer.files))
  }
}

// 上传文件
const uploadFiles = async (files: File[]) => {
  // 过滤文件
  const validFiles = files.filter(file => {
    if (file.size > 5 * 1024 * 1024) {
      message.error(`${file.name} 超过5MB限制`)
      return false
    }
    return true
  })
  
  if (validFiles.length === 0) return
  
  uploading.value = true
  uploadProgress.value = 0
  
  try {
    // 逐个上传文件
    for (const file of validFiles) {
      // 模拟上传进度
      const interval = setInterval(() => {
        if (uploadProgress.value < 90) {
          uploadProgress.value += 10
        } else {
          clearInterval(interval)
        }
      }, 200)
      
      // 实际上传文件
      const response = await fileApi.uploadImage(file) // 使用 uploadImage 而不是 upload
      
      clearInterval(interval)
      uploadProgress.value = 100
      
      // 添加到已上传文件列表
      uploadedFiles.value.push({
        name: file.name,
        size: file.size,
        url: response.data.filePath // 使用 filePath 而不是 url
      })
    }
    
    message.success('文件上传成功')
  } catch (error: any) {
    message.error(error.message || '文件上传失败')
  } finally {
    uploading.value = false
    uploadProgress.value = 0
    if (fileInput.value) {
      fileInput.value.value = ''
    }
  }
}

// 格式化文件大小
const formatFileSize = (bytes: number) => {
  if (bytes < 1024) return bytes + ' B'
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'
  return (bytes / (1024 * 1024)).toFixed(1) + ' MB'
}
</script>

<style scoped>
.file-upload-container {
  padding: 24px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.upload-area {
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  padding: 40px 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.3s;
  margin-bottom: 24px;
}

.upload-area:hover {
  border-color: #1890ff;
}

.upload-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.upload-icon {
  font-size: 48px;
  color: #1890ff;
}

.upload-text {
  font-size: 16px;
  color: #262626;
  margin: 0;
}

.upload-hint {
  color: #8c8c8c;
  margin: 0;
}

.file-input {
  display: none;
}

.upload-progress {
  text-align: center;
  margin-bottom: 24px;
}

.uploaded-files h3 {
  margin: 0 0 16px 0;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
}

.file-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.file-item {
  display: flex;
  gap: 16px;
  padding: 16px;
  border: 1px solid #d9d9d9;
  border-radius: 8px;
}

.file-preview {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: 4px;
}

.file-info {
  flex: 1;
}

.file-name {
  margin: 0 0 8px 0;
  font-weight: 500;
  color: #262626;
}

.file-size {
  margin: 0 0 8px 0;
  font-size: 14px;
  color: #8c8c8c;
}

.file-link {
  color: #1890ff;
  text-decoration: none;
}

.file-link:hover {
  text-decoration: underline;
}
</style>